
package com.framework.web.sys.repository.specifications;

import com.framework.core.util.LikePatternUtil;
import com.framework.core.util.StringUtil;
import com.framework.web.sys.domain.Role;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

public class RoleSpecs {

	/**
	 * 根据条件对角色进行查询分页操作
	 *
	 * @param role
	 * @return
	 */
	public static Specification<Role> LikeRoleName(
			final Role role) {

		return new Specification<Role>() {
			@Override
			public Predicate toPredicate(Root<Role> root,
					CriteriaQuery<?> query, CriteriaBuilder builder) {
				Predicate predicate = builder.and();
				//角色名称
				if (StringUtil.isNotEmpty(role.getRoleName())) {
					predicate = builder.and(predicate, builder.like(
							builder.lower(root.<String>get("roleName")),
							LikePatternUtil.getLikePattern(role.getRoleName())));
				}
				//状态
				if (StringUtil.isNotEmpty(role.getStatus())) {
					predicate = builder.and(predicate, builder.equal(
							builder.lower(root.<String>get("status")),role.getStatus()));
				}

				return predicate;
			}
		};
	}



}
